#!/usr/bin/python -tt
# -*- coding: utf-8 -*-

import sys
import os
import re
import math

# Format:
# S: 12 I: 13
# ====
# S: -23 I: 23
# ====
# ...


def main():
  benchmark_folder = "."
  logfile_raw = open("/var/tmp/douglas/spec_results.csv", 'w+')
  logfile_raw.write('Benchmark Name,[N^2, \infty], [2N, N^2], [N, 2N], [N, N],[N, N/2],[N/2, sqrt(N)],[sqrt(N), 0], Undef\n')

  logfile = open("/var/tmp/douglas/spec_results.csv", 'w+')
  logfile.write('Benchmark Name,[N^2, \infty], [2N, N^2], [N, 2N], [N, N], [N, N/2],[N/2, sqrt(N)],[sqrt(N), 0], Undef\n')
  for f in os.listdir(benchmark_folder):
    if f.endswith('.err'):
      print "Processing " + f
      benchmark_name = os.path.splitext(f)[0].replace('.linked', '')
      logfile.write(benchmark_name + ',')
      sub    = 0.0
      inc    = 0.0
      ns_inf = 0.0
      n2_2n  = 0.0
      n_2n   = 0.0
      n_n    = 0.0
      n_n2   = 0.0
      n2_vn  = 0.0
      vn_1   = 0.0
      err    = 0.0
      total  = 0
      for line in open(f, 'r'):
        match = re.search(r'S:(.+?)(\d{1,})(.+?)I:\s(\d{1,})', line)
        if match:
          total += 1
          sub  = abs(int(match.group(2)))
          inc  = abs(int(match.group(4)))

          #print sub, inc
          if sub > inc * inc: # [N^2, \infty]
            ns_inf += 1
          elif sub <= inc * inc and sub > 2 * inc: # [2N, N^2]
            n2_2n += 1
          elif sub <= 2 * inc and sub > inc + 1: # [N, 2N]
            n_2n += 1
          elif sub == inc or sub + 1 == inc or sub == inc + 1: # [N, N]
            n_n += 1
          elif sub > inc / 2 and sub < inc: # [N, N/2]
            n_n2 += 1
          elif sub <= inc / 2 and sub > math.sqrt(inc): # [N/2, sqrt(N)]
            n2_vn += 1
          elif sub <= math.sqrt(inc) and sub >= 0: # [sqrt(N), 0]
            vn_1 += 1
          else: # Undef
            err += 1

      ns_inf_perc = (float) ((ns_inf * 100)  / total) if ns_inf != 0 else 0
      n2_2n_perc  = (float) ((n2_2n * 100)  / total) if n2_2n != 0 else 0
      n_2n_perc   = (float) ((n_2n * 100)  / total) if n_2n != 0 else 0
      n_n_perc    = (float) ((n_n * 100)   / total) if n_n != 0 else 0
      n_n2_perc   = (float) ((n_n2 * 100)  / total) if n_n2 != 0 else 0
      n2_vn_perc  = (float) ((n2_vn * 100) / total) if n2_vn != 0 else 0
      vn_1_perc   = (float) ((vn_1 * 100)  / total) if vn_1 != 0 else 0
      err_perc    = (float) ((err * 100) / total) if err != 0 else 0

      print ns_inf_perc, n2_2n_perc, n_2n_perc, n_n_perc, n_n2_perc, n2_vn_perc, vn_1_perc, err_perc
      logfile.write(str(ns_inf_perc) + '%,' + str(n2_2n_perc) + '%,' + str(n_2n_perc) + '%,' + 
                    str(n_n_perc) + '%,' + str(n_n2_perc) + '%,' + str(n2_vn_perc) + '%,' + 
                    str(vn_1_perc) + '%,' + str(err_perc) + '%\n')

      logfile_raw.write(str(ns_inf) + ',' + str(n2_2n) + ',' + str(n_2n) + ',' + str(n_n) + ',' 
                    + str(n_n2) + ',' + str(n2_vn) + ',' + str(vn_1) + ',' + str(err) + '\n')

  logfile.close()    
  logfile_raw.close()

if __name__ == '__main__':
  main()


